package com.larissa.quiz;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavDirections;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class QuestionListFragment extends Fragment {
    private static final String TAG="QuestionListFragment";
    private QuestionViewModel viewmodel;
    private RecyclerView recyclerView;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        viewmodel=new ViewModelProvider(requireActivity()).get(QuestionViewModel.class);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        super.onCreateView(inflater,container,savedInstanceState);
        View view=inflater.inflate(R.layout.fragment_question_list,container,false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView=(RecyclerView) view.findViewById(R.id.question_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        viewmodel.questionsLiveData.observe(getViewLifecycleOwner(), new Observer<List<Question>>() {
            @Override
            public void onChanged(List<Question> questions) {
                updateUI(questions);
            }
        });
    }

    private void updateUI(List<Question>questions){
        QuestionItemAdapter adapter=new QuestionItemAdapter(questions);
        recyclerView.setAdapter(adapter);
    }

    private class QuestionItemHolder extends RecyclerView.ViewHolder{
        private TextView questionText;
        private TextView questionAnswer;
        private Question question;
        public QuestionItemHolder(View view){
            super(view);
            questionText=view.findViewById(R.id.question_text);
            questionAnswer=view.findViewById(R.id.question_answer);
            view.setOnClickListener(itemView->{
                NavDirections action=QuestionListFragmentDirections.actionShowQuestion(question.getQuestionId());
                Navigation.findNavController(itemView).navigate(action);
            });
        }

        public void bind(Question question){
            this.question=question;
            questionText.setText(question.getQuestionText());
            questionAnswer.setText(String.valueOf(question.isQuestionAnswer()));
        }
    }

    private class QuestionItemAdapter extends RecyclerView.Adapter<QuestionItemHolder>{
        private List<Question> questions;

        public QuestionItemAdapter(List<Question>questions){
            this.questions=questions;
        }

        @NonNull
        @Override
        public QuestionItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view=getLayoutInflater().inflate(R.layout.question_item,parent,false);
            return new QuestionItemHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull QuestionItemHolder holder, int position) {
            Question question=questions.get(position);
            holder.bind(question);
        }

        @Override
        public int getItemCount() {
            return questions.size();
        }
    }
}
